package com.cloud.mall.conf;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Author: 梁二东
 * @Date: 2025/1/7 22:37
 * @Description: 数据源配置
 **/
@Configuration
@MapperScan(basePackages = {"com.cloud.mall.mapper", "com.cloud.mall.test.mapper"}, sqlSessionTemplateRef = "productSqlSessionTemplate")
public class ProductDataSourceConfig {

    @Resource
    private ProductDataSourceProperties productDataSourceProperties;

    @Bean("productDatasource")
    @Primary
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(productDataSourceProperties.getDriverClassName());
        dataSource.setUrl(productDataSourceProperties.getUrl());
        dataSource.setUsername(productDataSourceProperties.getUsername());
        dataSource.setPassword(productDataSourceProperties.getPassword());

        // 配置连接池参数
        dataSource.setInitialSize(productDataSourceProperties.getInitialSize());
        dataSource.setMinIdle(productDataSourceProperties.getMinIdle());
        dataSource.setMaxActive(productDataSourceProperties.getMaxActive());
        dataSource.setMaxWait(productDataSourceProperties.getMaxWait());

        // 测试连接
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(true);

        // 配置其他属性可以根据需要添加

        return dataSource;
    }

    @Bean(name = "productSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("productDatasource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactory = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactory.setDataSource(dataSource);
        mybatisSqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
        // MybatisConfiguration 配置
        //mybatisSqlSessionFactory.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis/mybatis-config.xml"));
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        mybatisSqlSessionFactory.setConfiguration(mybatisConfiguration);
        // 添加分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql"); // 根据数据库类型设置
        properties.setProperty("reasonable", "true");
        pageInterceptor.setProperties(properties);
        mybatisSqlSessionFactory.setPlugins(pageInterceptor);
        return mybatisSqlSessionFactory.getObject();
    }

    /**
     * 需要手动配置事务管理器
     *
     * @param dataSource
     * @return
     */
    @Bean("productTransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("productDatasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "productSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate shardingSqlSessionTemplate(@Qualifier("productSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
